package com.crm.service.client.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.druid.util.StringUtils;
import com.crm.dao.client.ClientInfoDao;
import com.crm.dao.client.ContactDao;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.Contact;
import com.crm.service.client.ContactService;
import com.crm.vo.ContactIdAndNameVo;
import com.crm.vo.GetPinyin;
import com.crm.vo.Result;
import com.crm.vo.WordAndFirstName;

@Service
@Transactional(rollbackFor = Exception.class)
public class ContactServiceImpl implements ContactService {

	@Autowired
	public ContactDao contactDao;
	@Autowired
	public ClientInfoDao clientInfoDao;

	@Override
	public List<Contact> findAllContactsNoDel(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return contactDao.findAllContactsNoDel(page);
	}

	@Override
	public Long countContactsNoDel() {
		return contactDao.countContactsNoDel();
	}

	@Override
	public Result delContact(Long contactid) {
		try {
			if (contactid == null)
				return new Result("亲，请选择您要删除的联系人！");
			Contact contact = contactDao.findOne(contactid);
			contact.setIsDel("是");
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("亲，删除失败！");
		}

	}

	@Override
	public List<WordAndFirstName> getContactFirstName() {
		List<Contact> contacts = contactDao.findAllContactsNoDel(null);
		List<WordAndFirstName> wordAndFirstNames = new ArrayList<>();
		/* Map map = new HashMap<>(); */
		List<String> A = new ArrayList<>();
		List<String> B = new ArrayList<>();
		List<String> C = new ArrayList<>();
		List<String> D = new ArrayList<>();
		List<String> E = new ArrayList<>();
		List<String> F = new ArrayList<>();
		List<String> G = new ArrayList<>();
		List<String> H = new ArrayList<>();
		List<String> I = new ArrayList<>();
		List<String> J = new ArrayList<>();
		List<String> K = new ArrayList<>();
		List<String> L = new ArrayList<>();
		List<String> M = new ArrayList<>();
		List<String> N = new ArrayList<>();
		List<String> O = new ArrayList<>();
		List<String> P = new ArrayList<>();
		List<String> Q = new ArrayList<>();
		List<String> R = new ArrayList<>();
		List<String> S = new ArrayList<>();
		List<String> T = new ArrayList<>();
		List<String> U = new ArrayList<>();
		List<String> V = new ArrayList<>();
		List<String> W = new ArrayList<>();
		List<String> X = new ArrayList<>();
		List<String> Y = new ArrayList<>();
		List<String> Z = new ArrayList<>();
		for (Contact contact : contacts) {
			String contactName = contact.getContactName();
			String word = new GetPinyin().getStringFirstChar(contactName);
			if (word.length() > 0) {
				switch (word.charAt(0)) {
				case 'A':
					if (!A.contains(contactName.charAt(0) + ""))
						A.add(contactName.charAt(0) + "");
					break;
				case 'B':
					if (!B.contains(contactName.charAt(0) + ""))
						B.add(contactName.charAt(0) + "");
					break;
				case 'C':
					if (!C.contains(contactName.charAt(0) + ""))
						C.add(contactName.charAt(0) + "");
					break;
				case 'D':
					if (!D.contains(contactName.charAt(0) + ""))
						D.add(contactName.charAt(0) + "");
					break;
				case 'E':
					if (!E.contains(contactName.charAt(0) + ""))
						E.add(contactName.charAt(0) + "");
					break;
				case 'F':
					if (!F.contains(contactName.charAt(0) + ""))
						F.add(contactName.charAt(0) + "");
					break;
				case 'G':
					if (!G.contains(contactName.charAt(0) + ""))
						G.add(contactName.charAt(0) + "");
					break;
				case 'H':
					if (!H.contains(contactName.charAt(0) + ""))
						H.add(contactName.charAt(0) + "");
					break;
				case 'I':
					if (!I.contains(contactName.charAt(0) + ""))
						I.add(contactName.charAt(0) + "");
					break;
				case 'J':
					if (!J.contains(contactName.charAt(0) + ""))
						J.add(contactName.charAt(0) + "");
					break;
				case 'K':
					if (!K.contains(contactName.charAt(0) + ""))
						K.add(contactName.charAt(0) + "");
					break;
				case 'L':
					if (!L.contains(contactName.charAt(0) + ""))
						L.add(contactName.charAt(0) + "");
					break;
				case 'M':
					if (!M.contains(contactName.charAt(0) + ""))
						M.add(contactName.charAt(0) + "");
					break;
				case 'N':
					if (!N.contains(contactName.charAt(0) + ""))
						N.add(contactName.charAt(0) + "");
					break;
				case 'O':
					if (!O.contains(contactName.charAt(0) + ""))
						O.add(contactName.charAt(0) + "");
					break;
				case 'P':
					if (!P.contains(contactName.charAt(0) + ""))
						P.add(contactName.charAt(0) + "");
					break;
				case 'Q':
					if (!Q.contains(contactName.charAt(0) + ""))
						Q.add(contactName.charAt(0) + "");
					break;
				case 'R':
					if (!R.contains(contactName.charAt(0) + ""))
						R.add(contactName.charAt(0) + "");
					break;
				case 'S':
					S.add(contactName.charAt(0) + "");
					break;
				case 'T':
					if (!T.contains(contactName.charAt(0) + ""))
						T.add(contactName.charAt(0) + "");
					break;
				case 'U':
					if (!U.contains(contactName.charAt(0) + ""))
						U.add(contactName.charAt(0) + "");
					break;
				case 'V':
					if (!V.contains(contactName.charAt(0) + ""))
						V.add(contactName.charAt(0) + "");
					break;
				case 'W':
					if (!W.contains(contactName.charAt(0) + ""))
						W.add(contactName.charAt(0) + "");
					break;
				case 'X':
					if (!X.contains(contactName.charAt(0) + ""))
						X.add(contactName.charAt(0) + "");
					break;
				case 'Y':
					if (!Y.contains(contactName.charAt(0) + ""))
						Y.add(contactName.charAt(0) + "");
					break;
				case 'Z':
					if (!Z.contains(contactName.charAt(0) + ""))
						Z.add(contactName.charAt(0) + "");
					break;
				default:
					break;
				}
			}
		}
		wordAndFirstNames.add(new WordAndFirstName("A", A));
		wordAndFirstNames.add(new WordAndFirstName("B", B));
		wordAndFirstNames.add(new WordAndFirstName("C", C));
		wordAndFirstNames.add(new WordAndFirstName("D", D));
		wordAndFirstNames.add(new WordAndFirstName("E", E));
		wordAndFirstNames.add(new WordAndFirstName("F", F));
		wordAndFirstNames.add(new WordAndFirstName("G", G));
		wordAndFirstNames.add(new WordAndFirstName("H", H));
		wordAndFirstNames.add(new WordAndFirstName("I", I));
		wordAndFirstNames.add(new WordAndFirstName("J", J));
		wordAndFirstNames.add(new WordAndFirstName("K", K));
		wordAndFirstNames.add(new WordAndFirstName("L", L));
		wordAndFirstNames.add(new WordAndFirstName("M", M));
		wordAndFirstNames.add(new WordAndFirstName("N", N));
		wordAndFirstNames.add(new WordAndFirstName("O", O));
		wordAndFirstNames.add(new WordAndFirstName("P", P));
		wordAndFirstNames.add(new WordAndFirstName("Q", Q));
		wordAndFirstNames.add(new WordAndFirstName("R", R));
		wordAndFirstNames.add(new WordAndFirstName("S", S));
		wordAndFirstNames.add(new WordAndFirstName("T", T));
		wordAndFirstNames.add(new WordAndFirstName("U", U));
		wordAndFirstNames.add(new WordAndFirstName("V", V));
		wordAndFirstNames.add(new WordAndFirstName("W", W));
		wordAndFirstNames.add(new WordAndFirstName("X", X));
		wordAndFirstNames.add(new WordAndFirstName("Y", Y));
		wordAndFirstNames.add(new WordAndFirstName("Z", Z));
		return wordAndFirstNames;
	}

	@Override
	public List<Contact> findContactByNameNotDel(String contactName) {
		List<Contact> list = contactDao.findContactByNameNotDel(contactName + "%");
		return list;
	}

	@Override
	public Result addContact(Contact contact, Long cid) {
		try {
			ClientInfo clientInfo = clientInfoDao.findOne(cid);
			contact.setClientInfo(clientInfo);
			contact.setIsDel("否");
			contactDao.save(contact);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("亲，添加联系人失败");
		}

	}

	@Override
	public List<Contact> queryContactWhere(Contact contact, Long cid, int curpage, int pagesize) {
		Assert.notNull(contact);
		PageRequest page = new PageRequest(curpage - 1, pagesize, Sort.Direction.DESC, "contactid");
		Page<Contact> result = contactDao.findAll(new Specification<Contact>() {
			@SuppressWarnings("unchecked")
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery arg1, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				// 根据以下条件查询
				// 联系人编号
				if (contact.getContactid() != null) {
					list.add(cb.equal(root.get("contactid").as(Long.class), contact.getContactid()));
				}

				// 客户编号
				if (cid != null) {
					ClientInfo clientInfo = clientInfoDao.findOne(cid);
					list.add(cb.equal(root.get("clientInfo").as(ClientInfo.class), clientInfo));
				}

				// 联系人姓名
				if (!StringUtils.isEmpty(contact.getContactName())) {
					list.add(cb.like(root.get("contactName").as(String.class), "%" + contact.getContactName() + "%"));
				}

				// 性别
				if (!StringUtils.isEmpty(contact.getSex())) {
					list.add(cb.like(root.get("sex").as(String.class), "%" + contact.getSex() + "%"));
				}

				// 职务
				if (!StringUtils.isEmpty(contact.getJob())) {
					list.add(cb.equal(root.get("job").as(String.class), contact.getJob()));
				}

				// 部门
				if (!StringUtils.isEmpty(contact.getDept())) {
					list.add(cb.like(root.get("dept").as(String.class), "%" + contact.getDept() + "%"));
				}

				// 负责业务
				if (!StringUtils.isEmpty(contact.getTranscation())) {
					list.add(cb.like(root.get("transcation").as(String.class), "%" + contact.getTranscation() + "%"));
				}

				// 类型
				if (!StringUtils.isEmpty(contact.getType())) {
					list.add(cb.equal(root.get("type").as(String.class), contact.getType()));
				}

				// 电话
				if (!StringUtils.isEmpty(contact.getPhone())) {
					list.add(cb.equal(root.get("phone").as(String.class), contact.getPhone()));
				}

				// 传真
				if (!StringUtils.isEmpty(contact.getFax())) {
					list.add(cb.equal(root.get("fex").as(String.class), contact.getFax()));
				}

				// 邮件
				if (!StringUtils.isEmpty(contact.getEmail())) {
					list.add(cb.equal(root.get("email").as(String.class), contact.getEmail()));
				}

				// Qq
				if (!StringUtils.isEmpty(contact.getQq())) {
					list.add(cb.equal(root.get("qq").as(String.class), contact.getQq()));
				}

				// 家庭电话
				if (!StringUtils.isEmpty(contact.getHomePhone())) {
					list.add(cb.equal(root.get("homePhone").as(String.class), contact.getHomePhone()));
				}

				// 家庭电话
				if (!StringUtils.isEmpty(contact.getAddress())) {
					list.add(cb.like(root.get("address").as(String.class), "%" + contact.getAddress() + "%"));
				}

				// 联系人分类
				if (contact.getImportant() != null) {
					list.add(cb.equal(root.get("important").as(BigDecimal.class), contact.getImportant()));
				}

				// 证件类型
				if (!StringUtils.isEmpty(contact.getCardType())) {
					list.add(cb.equal(root.get("cardType").as(String.class), contact.getCardType()));
				}

				// 证件号码
				if (!StringUtils.isEmpty(contact.getCardNumber())) {
					list.add(cb.equal(root.get("cardNumber").as(String.class), contact.getCardNumber()));
				}

				// 微信
				if (!StringUtils.isEmpty(contact.getWechat())) {
					list.add(cb.equal(root.get("wechat").as(String.class), contact.getWechat()));
				}

				// 备注
				if (!StringUtils.isEmpty(contact.getRemarks())) {
					list.add(cb.like(root.get("remarks").as(String.class), "%" + contact.getRemarks() + "%"));
				}

				// 没有被删除
				list.add(cb.notLike(root.get("isDel").as(String.class), "是"));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		}, page);

		List<Contact> list = result.getContent();
		return list;

	}

	@Override
	public Long queryCountContactWhere(Contact contact, Long cid) {
		Assert.notNull(contact);

		Long count = contactDao.count(new Specification<Contact>() {
			@SuppressWarnings("unchecked")
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery arg1, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				// 根据以下条件查询
				// 根据以下条件查询
				// 联系人编号
				if (contact.getContactid() != null) {
					list.add(cb.equal(root.get("contactid").as(Long.class), contact.getContactid()));
				}

				// 客户编号
				if (cid != null) {
					ClientInfo clientInfo = clientInfoDao.findOne(cid);
					list.add(cb.equal(root.get("clientInfo").as(ClientInfo.class), clientInfo));
				}

				// 联系人姓名
				if (!StringUtils.isEmpty(contact.getContactName())) {
					list.add(cb.like(root.get("contactName").as(String.class), "%" + contact.getContactName() + "%"));
				}

				// 性别
				if (!StringUtils.isEmpty(contact.getSex())) {
					list.add(cb.like(root.get("sex").as(String.class), "%" + contact.getSex() + "%"));
				}

				// 职务
				if (!StringUtils.isEmpty(contact.getJob())) {
					list.add(cb.equal(root.get("job").as(String.class), contact.getJob()));
				}

				// 部门
				if (!StringUtils.isEmpty(contact.getDept())) {
					list.add(cb.like(root.get("dept").as(String.class), "%" + contact.getDept() + "%"));
				}

				// 负责业务
				if (!StringUtils.isEmpty(contact.getTranscation())) {
					list.add(cb.like(root.get("transcation").as(String.class), "%" + contact.getTranscation() + "%"));
				}

				// 类型
				if (!StringUtils.isEmpty(contact.getType())) {
					list.add(cb.equal(root.get("type").as(String.class), contact.getType()));
				}

				// 电话
				if (!StringUtils.isEmpty(contact.getPhone())) {
					list.add(cb.equal(root.get("phone").as(String.class), contact.getPhone()));
				}

				// 传真
				if (!StringUtils.isEmpty(contact.getFax())) {
					list.add(cb.equal(root.get("fex").as(String.class), contact.getFax()));
				}

				// 邮件
				if (!StringUtils.isEmpty(contact.getEmail())) {
					list.add(cb.equal(root.get("email").as(String.class), contact.getEmail()));
				}

				// Qq
				if (!StringUtils.isEmpty(contact.getQq())) {
					list.add(cb.equal(root.get("qq").as(String.class), contact.getQq()));
				}

				// 家庭电话
				if (!StringUtils.isEmpty(contact.getHomePhone())) {
					list.add(cb.equal(root.get("homePhone").as(String.class), contact.getHomePhone()));
				}

				// 家庭电话
				if (!StringUtils.isEmpty(contact.getAddress())) {
					list.add(cb.like(root.get("address").as(String.class), "%" + contact.getAddress() + "%"));
				}

				// 联系人分类
				if (contact.getImportant() != null) {
					list.add(cb.equal(root.get("important").as(BigDecimal.class), contact.getImportant()));
				}

				// 证件类型
				if (!StringUtils.isEmpty(contact.getCardType())) {
					list.add(cb.equal(root.get("cardType").as(String.class), contact.getCardType()));
				}

				// 证件号码
				if (!StringUtils.isEmpty(contact.getCardNumber())) {
					list.add(cb.equal(root.get("cardNumber").as(String.class), contact.getCardNumber()));
				}

				// 微信
				if (!StringUtils.isEmpty(contact.getWechat())) {
					list.add(cb.equal(root.get("wechat").as(String.class), contact.getWechat()));
				}

				// 备注
				if (!StringUtils.isEmpty(contact.getRemarks())) {
					list.add(cb.like(root.get("remarks").as(String.class), "%" + contact.getRemarks() + "%"));
				}

				// 没有被删除
				list.add(cb.notLike(root.get("isDel").as(String.class), "是"));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
		return count;
	}

	@Override
	public List<ContactIdAndNameVo> findContactIdAndName() {
		return contactDao.findContactIdAndName();
	}

	@Override
	public List<ContactIdAndNameVo> findContactIdAndNameByCid(Long cid) {
		return contactDao.findContactIdAndNameByCid(cid);
	}

	@Override
	public List<ClientInfo> findClientInfoBycontactid(Long contactid) {
		if (contactid != null) {
			Contact contact =contactDao.findOne(contactid);
			List<ClientInfo> list = new ArrayList<>();
			list.add(contact.getClientInfo());
			return list;
		}else {
			return null;
		}
	}
	//查询根据采购商id
	public List<Contact> findAllContactByMh(Long cid){
		return contactDao.findAllBycnameAndcidBymh(cid);
	}

	@Override
	public Contact findContactBycontactid(Long contactid) {
		if (contactid != null) {
			Contact contact =contactDao.findOne(contactid);
			return contact;
		}else {
			return null;
		}
	}


}
